<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include './classes/dnspod.php';
include './classes/mcrypt.php';
$config = include './config.inc.php';

//echo Helper_Mcrypt::encode($config['username'], $config['key'])."<br />";
//echo Helper_Mcrypt::encode($config['password'], $config['key'])."<br />";
//exit;

printf("TIME:%s\r\n", date('Y-m-d H:i:s'));

//获取公网IP
$ip = '';
$show_ip_servers = array(
    'http://iframe.ip138.com/ic.asp',
    'http://www.cz88.net/ip/viewip468_25_utf8.aspx',
);
foreach($show_ip_servers as $show_ip_server) {
    $ip_text = strip_tags(file_get_contents($show_ip_server));
    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $ip_text, $ip)) {
        $ip = $ip[0];
        break;
    }
}
unset($show_ip_servers, $show_ip_server);
printf("IP:%s\r\n", $ip);

//获取域名状态
$dnspod = new Dnspod(
    Helper_Mcrypt::decode($config['username'], $config['key']),
    Helper_Mcrypt::decode($config['password'], $config['key'])
);
$bs148_www = $dnspod->monitorListsubvalue($config['domain'], $config['record']);
$record = reset($bs148_www['records']);

//更新DDNS
if($record['value'] != $ip) {
    $dnspod->recordDdns($bs148_www['domain']['id'], $record['id'], $config['record'], '默认');
    echo "UPDATE\r\n";
} else {
    echo "IGNORE\r\n";
}